package com.cooba.activitys.user;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.dialog.CustomProgressDialog;
import com.mob.tools.utils.UIHandler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2016/7/5.
 */
public class ShareActivity extends AppCompatActivity implements View.OnClickListener, PlatformActionListener,Handler.Callback {
    private static File file;
    private static String dbPathAndName;
    private ImageButton mBack_btn;
    private TextView mShare_wechatmoment;
    private TextView mShare_wechatfriend;
    private TextView mShare_qq;
    private TextView mShare_qqzone;
    private TextView mShare_sinaweibo;
    private Context mContext;
    private String title;
    private String url;
    private String text;
    private Bitmap bitmap;

    private static final int MSG_TOAST = 1;
    private static final int MSG_ACTION_CCALLBACK = 2;
    private static final int MSG_CANCEL_NOTIFY = 3;
    private CustomProgressDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        mContext = this;
        initView();
    }

    private void initView() {
        mBack_btn = (ImageButton) findViewById(R.id.Share_image_back);
        mShare_wechatmoment = (TextView) findViewById(R.id.share_wechatmoment_tv);
        mShare_wechatfriend = (TextView) findViewById(R.id.share_wechatfriend_tv);
        mShare_qq = (TextView) findViewById(R.id.share_qq_tv);
        mShare_qqzone = (TextView) findViewById(R.id.share_qzone_tv);
        mShare_sinaweibo = (TextView) findViewById(R.id.share_sinaweibo_tv);
        mBack_btn.setOnClickListener(this);
        mShare_wechatmoment.setOnClickListener(this);
        mShare_wechatfriend.setOnClickListener(this);
        mShare_qq.setOnClickListener(this);
        mShare_qqzone.setOnClickListener(this);
        mShare_sinaweibo.setOnClickListener(this);
        ShareSDK.initSDK(mContext);
        initData();
    }

    private void initData() {
        title = "知行安徽";
        url = "http://tbs.iucool.cn:8080/cooba/cooba.html";
        text = "";
        dbPathAndName=copyToSD(mContext);
    }

    @Override
    public void onClick(View v) {
        dialog = CustomProgressDialog.createDialog(mContext);
        dialog.showDialog();
        switch (v.getId()) {
            case R.id.share_wechatmoment_tv:
                shareWechatmoment();
                break;
            case R.id.share_wechatfriend_tv:
                shareWechatfrient();
                break;
            case R.id.share_qq_tv:
                shareQQ();
                break;
            case R.id.share_qzone_tv:
                shareQzone();
                break;
            case R.id.share_sinaweibo_tv:
                shareSinaweibo();
                break;
            case R.id.Share_image_back:
                finish();
                break;
        }
    }

    private void shareSinaweibo() {
        SinaWeibo.ShareParams sinaweiboParams=new SinaWeibo.ShareParams();
        sinaweiboParams.setText(url);
        sinaweiboParams.setImagePath(dbPathAndName);
        Platform sinaweibo = ShareSDK.getPlatform(this, SinaWeibo.NAME);
        sinaweibo.setPlatformActionListener(ShareActivity.this);
        sinaweibo.share(sinaweiboParams);
    }

    private void shareQzone() {
        cn.sharesdk.tencent.qzone.QZone.ShareParams qzonepParams = new cn.sharesdk.tencent.qzone.QZone.ShareParams();
        qzonepParams.setTitle(title);
        qzonepParams.setTitleUrl(url);
        qzonepParams.setText(text);
        //qzonepParams.setImageUrl(imageUrl);
        qzonepParams.setImagePath(dbPathAndName);
        qzonepParams.setSite(title);
        qzonepParams.setSiteUrl(url);
        Platform qzone = ShareSDK.getPlatform(this, QZone.NAME);
        qzone.setPlatformActionListener(ShareActivity.this);
        qzone.share(qzonepParams);
    }

    private void shareQQ() {
        cn.sharesdk.tencent.qq.QQ.ShareParams qqParams = new cn.sharesdk.tencent.qq.QQ.ShareParams();
        qqParams.setTitle(title);
        qqParams.setTitleUrl(url);
        qqParams.setText(text);
        //qqParams.setImageUrl(imageUrl);
        qqParams.setImagePath(dbPathAndName);
        Platform qq = ShareSDK.getPlatform(this, QQ.NAME);
         qq.setPlatformActionListener(ShareActivity.this);
        qq.share(qqParams);
    }

    private void shareWechatfrient() {
        Wechat.ShareParams wechat = new Wechat.ShareParams();
        wechat.setTitle(title);
        wechat.setText(text);
        //wechat.setImageUrl(imageUrl);
        wechat.setImagePath(dbPathAndName);
        wechat.setUrl(url);
        wechat.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(this, Wechat.NAME);
        weixin.setPlatformActionListener(ShareActivity.this);
        weixin.share(wechat);
    }

    private void shareWechatmoment() {
        WechatMoments.ShareParams wechatMoments = new WechatMoments.ShareParams();
        wechatMoments.setTitle(title);
        wechatMoments.setText(text);
        wechatMoments.setUrl(url);
        //wechatMoments.setImageUrl(imageUrl);
        wechatMoments.setImagePath(dbPathAndName);
        wechatMoments.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(this, WechatMoments.NAME);
        weixin.setPlatformActionListener(ShareActivity.this);
        weixin.share(wechatMoments);
    }

    public static String copyToSD(Context context) {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            String path = Environment.getExternalStorageDirectory()
                    .getPath();
            path = path + "/cooba";

            dbPathAndName = path + "/" + "shareicon.png";

            file = new File(path);

            if (file.exists() == false) {

                file.mkdir();
            }

            File dbFile = new File(dbPathAndName);
            if (!dbFile.exists()) {
                is = context.getResources().openRawResource(
                        R.raw.shareicon);
                fos = new FileOutputStream(dbFile);

                byte[] buffer = new byte[8 * 1024];// 8K
                while (is.read(buffer) > 0)// >
                {
                    fos.write(buffer);
                }
            }

        } catch (Exception e) {

        } finally {
            try {
                if (is != null) {
                    is.close();
                }

                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
        }
        return dbPathAndName;
    }

    @Override
    public void onCancel(Platform platform, int action) {
        // dialog.closeDialog();
        // 取消
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 3;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);

    }

    @Override
    public void onComplete(Platform platform, int action,
                           HashMap<String, Object> arg2) {
        // dialog.closeDialog();
        // 成功
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 1;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);

    }

    @Override
    public void onError(Platform platform, int action, Throwable t) {
        //dialog.closeDialog();
        // 失敗
        // 打印错误信息,print the error msg
        t.printStackTrace();
        // 错误监听,handle the error msg
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 2;
        msg.arg2 = action;
        msg.obj = t;
        UIHandler.sendMessage(msg, this);

    }

    public boolean handleMessage(Message msg) {
        switch (msg.arg1) {
            case 1: {
                // 成功
                Toast.makeText(mContext, "分享成功", Toast.LENGTH_SHORT).show();
            }
            break;
            case 2: {
                // 失败
                Toast.makeText(mContext, "分享失败", Toast.LENGTH_SHORT).show();
            }
            break;
            case 3: {
                // 取消
                Toast.makeText(mContext, "分享取消", Toast.LENGTH_SHORT).show();
            }
            break;
        }

        return false;

    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (dialog != null) {
            dialog.closeDialog();
        }
    }
}
